<template>
  <el-card shadow="hover" class="mt-20">
    <!-- 
      三级分类id的值
        三级分类id有值，取boolean为true, 希望按钮不禁用，得到false
        三级分类id没有值，取boolean为false, 希望按钮禁用，得到true
     -->
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddAttr"
    >
      添加属性
    </el-button>
    <el-table :data="attrList" border class="mt-20">
      <el-table-column type="index" label="序号" align="center" width="60" />
      <el-table-column label="属性名称" prop="attrName" width="150" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            type="success"
            class="mr-10"
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
          >
            {{ attrValue.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            @click="showUpdateAttr(row)"
            title="修改属性"
          />
          <el-popconfirm
            :title="`您确认要删除 ${row.attrName} 属性吗?`"
            @confirm="delAttr(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                :icon="Delete"
                size="small"
                title="删除属性"
              />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { watch, ref } from "vue";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { getAttrListApi, delAttrApi } from "@/api/product/attr";
import type { AttrList, AttrItem } from "@/api/product/model/attrModel";
import { useCategoryStore } from "@/stores/category";
import { useAttrStore } from "@/stores/attr";
import { ElMessage } from "element-plus";

// 删除属性
const delAttr = async (id: number) => {
  await delAttrApi(id);
  ElMessage.success("删除属性成功");
  getAttrList();
};

const emit = defineEmits(["setIsShowAttrList"]);

const attrStore = useAttrStore();

const showAddAttr = () => {
  emit("setIsShowAttrList", false);
  // attrStore.attr = {
  //   attrName: "",
  //   attrValueList: [],
  // };
  attrStore.$reset(); // 重新pinia这个模块数据，为初始化值
};

const showUpdateAttr = (row: AttrItem) => {
  emit("setIsShowAttrList", false);
  attrStore.attr = row;
};

const categoryStore = useCategoryStore();

const attrList = ref<AttrList>([]);
// 封装公共请求函数
const getAttrList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;

  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      attrList.value = [];
      return;
    }

    getAttrList();
  },
  {
    immediate: true,
  }
);
</script>

<style scoped></style>
